// 下方打出的牌汇总
class outPokerPace extends egret.Sprite{
    private GamePlayID:number;
    //玩家座位ID
    public constructor(GamePlayId:number=0){
        super();
        this.GamePlayID = GamePlayId;
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddEd,this);
    }
    private onAddEd(){
        this.init()
        this.initSmall()
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
    }
    private onRemove(){
    }

    private total:number = 18;
    private smallTotal:number = 24;
    private showNum:number = 0;
    private pokerList:Array<any>=new Array();
    private smallPokerList:Array<any>=new Array();
    private pokerDataList:Array<any>=new Array();
    private init(){
        this.initCenteroutPokerCache()
        for(var i=0;i<this.total;i++){
            var poker;
            if(this.GamePlayID == 1){//右方
                poker = new outPoker2();
                poker.x = Math.floor(i/6)*(poker.width-2);
                poker.y = ((poker.height-14)*(6-(i%6)))-poker.height;
            }else if(this.GamePlayID == 2){//上方
                poker = new outPoker3();
                poker.x = poker.width*(i%9);
                poker.y = Math.floor(i/9)*(poker.height-12);
            }else if(this.GamePlayID == 3){//左方
                poker = new outPoker4();
                poker.x = Math.floor(i/6)*(poker.width-2);
                poker.y = (poker.height-14)*(6-(i%6));
            }else{//下方
                poker = new outPoker1();
                poker.x = poker.width*(i%9);
                poker.y = Math.floor(i/9)*(poker.height-12);
            }
            poker.setContent("");
            this.addChild(poker);
            ArraySort.setDepth(poker,this);
            poker.visible = false;
            this.pokerList.push(poker);
        }
        if(this.GamePlayID == 2||this.GamePlayID == 3){
            this.pokerList.reverse();
        }
    }

    private initCenteroutPokerCache = _.once(function() {
        const pokerTypes = ['W', 'S', 'T']
        pokerTypes.forEach(type => {
            for (var i = 1; i <= 9; i++) {
                // let image = new eui.Image("X"+type+"_"+i+"_png")
                // console.log('image', image)
                GameData.centeroutPokerCache.push({
                    name: type + i,
                    texture: RES.getRes("X"+type+"_"+i+"_png")
                })
            }
        })
    })
    private initSmall(){
        for(var i=0;i<this.smallTotal;i++){
            var poker;
            if(this.GamePlayID == 1){//右方
                poker = new outPoker2();
                poker.x = Math.floor(i/6)*(poker.width-2);
                poker.y = ((poker.height-14)*(6-(i%6)))-poker.height;
            }else if(this.GamePlayID == 2){//上方
                poker = new outPoker3();
                poker.scaleX = poker.scaleY = .8
                poker.x = poker.width*.8*(i%10);
                poker.y = Math.floor(i/10)*(poker.height*.8-12);
            }else if(this.GamePlayID == 3){//左方
                poker = new outPoker4();
                poker.x = Math.floor(i/6 - 1)*(poker.width-2);
                poker.y = (poker.height-14)*(6-(i%6));
            }else{//下方
                poker = new outPoker1();
                poker.scaleX = poker.scaleY = .8
                poker.x = poker.width*.8*(i%10);
                poker.y = Math.floor(i/10)*(poker.height*.8-12);
            }
            poker.setContent("");
            // this.addChild(poker);
            ArraySort.setDepth(poker,this);
            poker.visible = false;
            this.smallPokerList.push(poker);
        }
        if(this.GamePlayID == 2||this.GamePlayID == 3){
            this.smallPokerList.reverse();
        }
    }
    //清除所有牌
    public clearAllPoker(){
        for(var i=0;i<this.total;i++){
            this.pokerList[i].setContent("");
            this.pokerList[i].visible = false;
        }
        this.smallPokerList.forEach(small => {
            small.setContent("");
            small.visible = false;
            if (small.parent) {
                this.removeChild(small)
            }
        })
        this.pokerDataList = [];
        this.showNum = 0;
    }
    //添加一张牌
    public addSinglePoker(flower:string,value:string,id:number){      
        console.log('this.showNum', this.showNum);
        console.log('this.GamePlayID', this.GamePlayID)
        if(this.showNum>=this.smallTotal){
            this.showNum = this.smallTotal-1;
        }
        if (this.pokerList[this.showNum]) {
            // this.pokerList[this.showNum].setContent("X"+flower+"_"+value+"_png");
            this.pokerList[this.showNum].setContent(flower + value);
            this.pokerList[this.showNum].visible = true
        }
        // this.smallPokerList[this.showNum].setContent("X"+flower+"_"+value+"_png");
        this.smallPokerList[this.showNum].setContent(flower + value);
        if(this.showNum >= this.total){
            this.pokerList.forEach(poker => {
                poker.visible = false
                // if (poker.parent) {
                //     this.removeChild(poker)
                // }
            })
            console.log('this.GamePlayID', this.GamePlayID);
            if(this.GamePlayID == 1||this.GamePlayID == 2){
                for (let n = this.smallPokerList.length - 1; n >= 0; n--) {
                    // this.addChild(this.smallPokerList[n])
                    this.smallPokerList[n].visible = n <= this.pokerDataList.length
                    if (n <= this.pokerDataList.length) {
                        this.addChild(this.smallPokerList[n])
                    }
                }
            } else {
                this.smallPokerList.forEach((poker, index) => {
                    this.addChild(poker)
                    poker.visible = index <= this.pokerDataList.length
                })
            }
        }
        this.pokerDataList.push({flower:flower,value:value,id:id}); 
        this.showNum = this.pokerDataList.length;
    }

    //删除一张牌
    public deletSinglePoker(){
        let lsConut:number = this.pokerDataList.length;
        //console.log("\n\n删除一张赢掉的牌："+lsConut);
        if(lsConut&&lsConut>0){     
            if(this.pokerList[lsConut-1]){
                // this.pokerList[lsConut-1].visible = false;
                this.pokerDataList.splice(lsConut-1,1); 
                this.showNum = lsConut-1; 
            }
        }
    }
    //获取最新出牌的位置
    public getOutPoketXY():any{
        var lsx:number;
        var lsy:number;
        
        if(this.showNum>=this.total){
            lsx = this.pokerList[this.total-1].x;
            lsy = this.pokerList[this.total-1].y;
        }else{
            if(this.showNum<1){
                lsx = this.pokerList[this.showNum].x;
                lsy = this.pokerList[this.showNum].y;
            }else{
                lsx = this.pokerList[this.showNum-1].x;
                lsy = this.pokerList[this.showNum-1].y;
            }
        }
         return {x:lsx,y:lsy,conut:this.showNum};//返回牌的坐标
    }

    public getOutCard():any{
        console.log('this.showNum', this.showNum);
        // console.log(this.pokerList, this.smallPokerList);
        if(this.showNum>this.total){
            return this.smallPokerList[this.showNum - 1]
        }
        return this.pokerList[this.showNum - 1]
    }

    public getOutNumber():any{
        return this.showNum
    }
    

    //显示查找的牌
    public showlookupPoker(pokerflower:number,pokerValue:number){
        for(var i=0;i<this.pokerDataList.length;i++){
            if(pokerflower == this.pokerDataList[i].flower && pokerValue == this.pokerDataList[i].value){
               this.pokerList[i].alpha = 0.5;            
            }
        }
    }
    //隐藏查找的牌
    public hidelookupPoker(){
        for(var i=0;i<this.pokerList.length;i++){
            this.pokerList[i].alpha = 1;
        }
    }
}